package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserVehicleSoul;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserVehicleSoulDaoImpl extends TempDataObjectDao<UserVehicleSoul> {

	public int getUserVehicleSoulMaxId(long userId) {
		String sql = "select max(soulId) as maxSoulId from t_u_vehicle_soul where userId=?";
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				return rs.getInt("maxSoulId") + 1;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return 1;
	}

	public Map<Integer, UserVehicleSoul> getUserVehicleSoulMap(long userId) {
		String sql = "select * from t_u_vehicle_soul where userId=? and isDelete=0";
		Map<Integer, UserVehicleSoul> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicleSoul data = parse(rs);
				dataMap.put(data.getSoulId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public Map<Integer, UserVehicleSoul> getUserVehicleSoulEquipMap(long userId) {
		String sql = "select * from t_u_vehicle_soul where userId=? and isDelete=0 and vehicleId != ''";
		Map<Integer, UserVehicleSoul> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setLong(1, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicleSoul data = parse(rs);
				dataMap.put(data.getSoulId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	@Override
	protected boolean addData(UserVehicleSoul data) {
		PreparedStatement call = null;
		String sql = "INSERT INTO t_u_vehicle_soul(userId,soulId,configId,vehicleId," +
				"equipTime,equipPos,isDelete,occList,entryMap,isLock) VALUES (?,?,?,?,?,?,?,?,?,?)";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getSoulId());
			call.setInt(i++, data.getConfigId());
			call.setString(i++, data.getVehicleId());
			call.setLong(i++, data.getEquipTime());
			call.setInt(i++, data.getEquipPos());
			call.setBoolean(i++, data.isDelete());
			call.setString(i++, StringUtils.listToString(data.getOccList(),";"));
			call.setString(i++, JSONObject.toJSONString(data.getEntryMap()));
			call.setBoolean(i++, data.isLock());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected boolean updateData(UserVehicleSoul data) {
		PreparedStatement call = null;
		String sql = "UPDATE t_u_vehicle_soul SET configId=?,vehicleId=?,equipTime=?," +
				"equipPos=?,isDelete=?,occList=?,entryMap=?,isLock=? WHERE userId=? AND soulId=?";
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareStatement(sql);
			int i = 1;
			call.setInt(i++, data.getConfigId());
			call.setString(i++, data.getVehicleId());
			call.setLong(i++, data.getEquipTime());
			call.setInt(i++, data.getEquipPos());
			call.setBoolean(i++, data.isDelete());
			call.setString(i++, StringUtils.listToString(data.getOccList(),";"));
			call.setString(i++, JSONObject.toJSONString(data.getEntryMap()));
			call.setBoolean(i++, data.isLock());

			call.setLong(i++, data.getUserId());
			call.setInt(i++, data.getSoulId());
			return call.executeUpdate() > 0;
		} catch (Exception e) {
			logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
		} finally {
			getDb().closeConnection(call, null);
		}
		return false;
	}

	@Override
	protected UserVehicleSoul parseData(ResultSet rs) throws SQLException {
		UserVehicleSoul data = new UserVehicleSoul();
		data.setUserId(rs.getLong("userId"));
		data.setSoulId(rs.getInt("soulId"));
		data.setConfigId(rs.getInt("configId"));
		data.setVehicleId(rs.getString("vehicleId"));
		data.setEquipTime(rs.getLong("equipTime"));
		data.setEquipPos(rs.getInt("equipPos"));
		data.setDelete(rs.getBoolean("isDelete"));
		String occListStr = rs.getString("occList");
		if(!StringUtils.isNullOrEmpty(occListStr)){
			data.setOccList(StringUtils.stringToIntegerList(occListStr,";"));
		}
		String entryMapStr = rs.getString("entryMap");
		if(!StringUtils.isNullOrEmpty(entryMapStr)){
			data.setEntryMap(JSON.parseObject(entryMapStr, new TypeReference<ConcurrentHashMap<Integer, List<Integer>>>() {}));
		}
		data.setLock(rs.getBoolean("isLock"));
		return data;
	}
}
